package com.ayh.campusfun.controller.api.v1;

import com.ayh.campusfun.common.CommonResult;
import com.ayh.campusfun.common.PageResult;
import com.ayh.campusfun.common.core.jwt.JWTUtils;
import com.ayh.campusfun.common.exception.http.ForbiddenException;
import com.ayh.campusfun.entity.Attention;
import com.ayh.campusfun.entity.LikeFunTopic;
import com.ayh.campusfun.entity.TopicShare;
import com.ayh.campusfun.entity.Users;
import com.ayh.campusfun.service.AttentionService;
import com.ayh.campusfun.service.LikeFunTopicService;
import com.ayh.campusfun.service.TopicShareService;
import com.ayh.campusfun.vo.TopicVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 话题表(TopicShare)表控制层
 *
 * @author ayh
 * @since 2021-03-20 20:42:42
 */
@RestController
@RequestMapping("topicShare")
public class TopicShareController {
    /**
     * 服务对象
     */
    @Autowired
    private TopicShareService topicShareService;

    @Autowired
    private LikeFunTopicService likeFunTopicService;

    @Autowired
    private AttentionService attentionService;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Resource
    private JWTUtils jwtUtils;

    /**
     * 获取趣事列表
     * @param limit
     * @param page
     * @return
     */
    @PostMapping(value = "/findTopicList")
    public CommonResult getFunList(
            HttpServletRequest request,
            @RequestParam(defaultValue = "15") Integer limit,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "1") Integer topicClassifyId){
        String uid = jwtUtils.getUidFromRequest(request);
        Map<String,Object> map = new HashMap<>();
        map.put("limit",limit);
        map.put("page",page);
        map.put("order","id desc");
        map.put("topicClassifyId",topicClassifyId);
        PageResult pageResult = topicShareService.findAll(map);

        return CommonResult.success(pageResult);
    }


    @PostMapping(value = "/searchTopicList")
    public CommonResult searchTopicList(
            @RequestParam(defaultValue = "15") Integer limit,
            @RequestParam(defaultValue = "1") Integer page,
            String keyWords){
        Map<String,Object> map = new HashMap<>();
        map.put("limit",limit);
        map.put("page",page);
        map.put("keyWords",keyWords);
        map.put("order","id desc");
        PageResult pageResult = topicShareService.findAll(map);

        return CommonResult.success(pageResult);
    }

    @PostMapping(value = "/findTopicShareDetail",produces = "application/json")
    public CommonResult findTopicShareDetail(HttpServletRequest request,Long funId){
        TopicShare topicShare = topicShareService.findById(funId);
        String uid = jwtUtils.getUidFromRequest(request);
        Map<String,Object> queryMap = new HashMap<>();
        boolean isClick = false;
        boolean isAttention = false;

        queryMap.put("uid",uid);
        queryMap.put("funId",funId);
        queryMap.put("funType",2);
        LikeFunTopic likeFunTopic = likeFunTopicService.findByParam(queryMap);

        queryMap.clear();
        queryMap.put("uid",uid);
        queryMap.put("attentionId",topicShare.getUid());
        Attention attention = attentionService.findByParam(queryMap);

        if(likeFunTopic!=null) {
            isClick = true;
        }
        if(attention!=null) {
            isAttention = true;
        }

        return CommonResult.success(TopicVO.build(topicShare,isClick,isAttention)) ;
    }

    @PostMapping(value = "/addShare",produces = "application/json")
    public CommonResult addShare(HttpServletRequest request,TopicShare share) {
        String authorization = jwtUtils.getTokenFromRequest(request);
        Users users = (Users) redisTemplate.opsForValue().get(authorization);
        if (users == null) {
            throw new ForbiddenException(60009);
        }

        share.setUid(users.getUid());
        share.setAvatarUrl(users.getAvatarUrl());
        share.setNickName(users.getNickName());

        topicShareService.insert(share);
        return CommonResult.successNoData();
    }
}
